<?php

require FCPATH.'/application/controllers/backend/backend.php';

class Permission_group extends Backend {

	public function __construct(){
		parent::__construct();
		$this->load->model('permission_group_model','model');
	}

	public function index(){
		$this->load_js(array('jquery-ui-1.8.20.custom.min','backend_permission_group_index'));
		$this->load_css(array( 'custom-theme/jquery-ui-1.8.20.custom'));
		$data = array('l_position' => array(array('name' => '权限分组')));
		$this->rend('permission_group_index',$data);
	}

	public function records(){
		$page = intval($this->input->get('page'));
		$page_size = intval($this->input->get('pageSize'));
		$page = $page?$page:1;
		$page_size = $page_size?$page_size:10;
		$offset = $page_size * ($page - 1);

		$name = trim($this->input->get('name'));
		$start = trim($this->input->get('start'));
		$end = trim($this->input->get('end'));

		$conditions = array();

		if (!empty($name)) {
			$conditions[] = array('like', 'name', $name);
		}

		if (!empty($start)) {
			$conditions[] = array('where', 'created_at >=', $start);
		}

		if (!empty($end)) {
			$conditions[] = array('where', 'created_at <=', $end);
		}

		$order_fields = array('name', 'created_at');
		$order_field = intval($this->input->get('order_field'));
		$order_type = intval($this->input->get('order_type'));
		$orders = array();
		if ($order_field >= 0 && $order_field < count($order_fields)) {
			$orders = array(
				array($order_fields[$order_field], $order_type == 1 ? 'asc' : 'desc')
			);
		}

		$this->json_back('XT001','',array(
			'total' => $this->model->total($conditions),
			'records' => $this->model->records(array(),$conditions,$orders,$page_size,$offset)
		));
	}

	public function add(){
		$this->load->config('global');

		$data = array(
			'all_permissions' => $this->config->item('all_permissions'),
			'l_position' => array(
				array(
					'path' => '/permission_group/index',
					'name' => '权限分组'
				),
				array(
					'name' => '添加权限分组'
				)
			)
		);

		$this->load_css('user_permission');
		$this->load_js('permission_group_add');
		$this->rend('permission_group_add',$data);
	}

	public function find(){
		$id = intval($this->input->get('id'));
		if(!($data = $this->model->find($id))){
			$this->json_back('XT002','该记录已被删除,或ID有误。');
		}
		$this->json_back('XT001','',$data);
	}

	public function insert(){
		$data = array(
			'name' => trim($this->input->post('name')),
			'path' => $this->input->post('path')
 		);

		$this->validate($data);

		$data['path'] = implode(',',$data['path']);

		if(($id = $this->model->insert($data)) > 0){
			$this->json_back('XT001','',array('id' => $id));
		}

		$this->json_back('XT002','数据库或SQL语句有误。');
	}

	public function edit(){
		$id = intval($this->uri->segment(4));

		if(!($permission_group = $this->model->find($id))){
			exit('该记录已被删除,或ID有误。');
		}

		$this->load->config('global');

		$permission_group->path = explode(',', $permission_group->path);

		$data = array(
			'all_permissions' => $this->config->item('all_permissions'),
			'l_position' => array(
				array(
					'path' => '/permission_group/index',
					'name' => '权限分组'
				),
				array(
					'name' => "编辑 <b>{$permission_group->name}</b> 权限分组"
				)
			),
			'permission_group' => $permission_group
		);

		$this->load_css('user_permission');

		$this->load_js('permission_group_edit');

		$this->rend('permission_group_edit',$data);
	}

	public function update(){
		$id = intval($this->input->post('id'));

		if(!$this->model->find($id)){
			$this->json_back('XT002','该记录已被删除,或ID有误。');
		}

		$data = array(
			'name' => trim($this->input->post('name')),
			'path' => $this->input->post('path')
 		);

		$this->validate($data ,$id);

		$data['path'] = implode(',',$data['path']);

		if($this->model->update($id,$data)){
			$this->json_back('XT001','修改成功');
		}

		$this->json_back('XT002','数据库或SQL语句有误。');
	}

	public function delete(){
		$id = intval($this->uri->segment(4));
		if($this->model->delete($id)){
			$this->json_back('XT001','删除成功。');
		}
		$this->json_back('XT002','数据库或SQL语句有误。');
	}

	private function validate($data, $id = 0){
		if(empty($data['name'])){
			$this->json_back('XT002','请填写权限分组名称');
		}

		$conditions = array(
			array('where','name',$data['name'])
		);

		if($id > 0){
			$conditions[] = array('where','id <>',$id);
		}

		if($this->model->total($conditions) > 0){
			$this->json_back('XT002','该分组名称已经存在，请更换一个。');
		}

		if($data['path'] === FALSE){
			$this->json_back('XT002','请选择权限');
		}
	}
}
